package com.simplenix.nicasio.admin.misc;

import com.simplenix.nicasio.admin.eb.Group;
import com.simplenix.nicasio.admin.eb.User;
import com.simplenix.nicasio.admin.eb.UsuGroup;
import com.simplenix.nicasio.hmaint.DefaultCustomUpdate;
import com.simplenix.nicasio.hmaint.EntityDefinition;
import com.simplenix.nicasio.hmaint.HMaintException;
import com.simplenix.nicasio.hmaint.util.HMaintUtil;
import com.simplenix.nicasio.misc.AppException;
import com.simplenix.nicasio.misc.Internationalization;
import com.simplenix.nicasio.misc.StrUtil;
import com.simplenix.nicasio.persistence.HibernateUtil;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author fronald
 */
public class UpdateGroup extends DefaultCustomUpdate {

	@Override
	public String getHtml(EntityDefinition ed, String id) throws AppException, HMaintException {
		Group g = (Group) HMaintUtil.findById(ed.getEntityClass(), id);
		String rtn = super.getHtml(ed, id);
		Criteria c = HibernateUtil.getCurrentSession().createCriteria(User.class);
		c.addOrder(Order.asc("name"));
		List<User> users = c.list();
		rtn += "<table>";
		rtn += "<th>" + Internationalization.getInstance().getString(User.class.getName()) + "</th>";
		rtn += "<th>" + Internationalization.getInstance().getString("member") + "</th>";
		for (User user : users) {
			if (!StrUtil.nvl(user.getLogin()).equals("admin")) {
				rtn += "<tr><td>" + StrUtil.nvl(user.getName()) + "</td>";
				String yselected = "";
				String nselected = "";
				if (UsuGroup.isMember(user, g)) {
					yselected = " checked ";
				} else {
					nselected = " checked ";
				}
				rtn += "<td><input type='radio' " + yselected + " value='Y' name='usu" + user.getUsuId() + "'>" + Internationalization.getInstance().getString("yes") + "</input><input type='radio' " + nselected + " value='N' name='usu" + user.getUsuId() + "'>" + Internationalization.getInstance().getString("not") + "</input></td></tr>";
			}
		}
		rtn += "</table>";
		return rtn;
	}

	@Override
	public void doUpdate(Map reg, EntityDefinition ed) throws AppException, HMaintException {
		Group g = (Group) HMaintUtil.findById(ed.getEntityClass(), (String) reg.get(ed.getKeyFieldName()));
		super.doUpdate(reg, ed);
		Criteria c = HibernateUtil.getCurrentSession().createCriteria(User.class);
		List<User> users = c.list();
		for (User user : users) {
			if (!StrUtil.nvl(user.getLogin()).equals("admin")) {
				String key = "usu" + user.getUsuId();
				if (reg.containsKey(key)) {
					String value = (String) reg.get(key);
					boolean isMember = value.equals("Y");
					if (UsuGroup.isMember(user, g)) {
						if (!isMember) {
							Criteria criteria = HibernateUtil.getCurrentSession().createCriteria(UsuGroup.class);
							criteria.add(Restrictions.eq("user", user));
							criteria.add(Restrictions.eq("group", g));
							List<UsuGroup> usuGroups = criteria.list();
							for (UsuGroup usug : usuGroups) {
								HibernateUtil.getCurrentSession().delete(usug);
							}
						}
					} else {
						if (isMember) {
							UsuGroup u = new UsuGroup();
							u.setGroup(g);
							u.setUser(user);
							HibernateUtil.getCurrentSession().save(u);
						}
					}
				}
			}
		}
	}

}
